package interfaces.clients.authorization;

import junit.framework.TestCase;
import interfaces.clients.authorization.exceptions.*;

/**
 * Authorization test 1.
 *
 * Before running the database must be deleted.
 *
 * @author Manujlov Maxim
 */
public class AuthorizationTest1 extends TestCase {
  private static final Authorizer a = AuthorizationSystem.getInstance();

  public void test101() {
    assertEquals(a.getUsersCount(), 0);
  }

  public void test102() {
    try {
      a.addUser(new AuthorizationData("Tom", "qwerty"));
    }
    catch (Throwable e) {
      fail();
    }
    assertEquals(a.getUsersCount(), 1);
  }

  public void test103() {
    try {
      a.addUser(new AuthorizationData("Jack", "123456"));
      a.deleteUser(new AuthorizationData("Jack", "123456"));
    }
    catch (Throwable e) {
      fail();
    }
    assertEquals(a.getUsersCount(), 1);
  }

  public void test104() {
    assertEquals(a.userExists("Tom"), true);
  }

  public void test105() {
    assertEquals(a.userExists("Jack"), false);
  }

  public void test106() {
    UserData user;
    try {
      user = a.authorize(new AuthorizationData("Tom", "qwerty"));
      assertEquals(user.getName(), "Tom");
      assertEquals(user.getPassword(), "qwerty");
    }
    catch (Throwable e) {
      fail();
    }
  }

  public void test107() {
    try {
      a.authorize(new AuthorizationData("Tom", "123456"));
    }
    catch (WrongPasswordException e) {
      return;
    }
    catch (Throwable e) {
      fail();
    }
    fail();
  }

  public void test108() {
    try {
      a.authorize(new AuthorizationData("Jack", "qwerty"));
    }
    catch (UserDoesNotExistException e) {
      return;
    }
    catch (Throwable e) {
      fail();
    }
    fail();
  }

  public void test109() {
    UserData user;
    try {
      user = a.addUser(new AuthorizationData("Jack", "123456"));
      assertEquals(user.getName(), "Jack");
      assertEquals(user.getPassword(), "123456");
    }
    catch (Throwable e) {
      fail();
    }
  }

  public void test110() {
    try {
      a.addUser(new AuthorizationData("Tom", "123456"));
    }
    catch (UserAlreadyExistsException e) {
      return;
    }
    catch (Throwable e) {
      fail();
    }
    fail();
  }

  public void test111() {
   // works for a long time
   /* String s = "";
    for (int i = 0; i < 65536; i++) s += "a";
    try {
      a.addUser(new AuthorizationData(s, "qwerty"));
    }
    catch (TooLongStringException e) {
      return;
    }
    catch (Throwable e) {
      fail();
    }
    fail(); */
  }

  public void test112() {
    try {
      a.deleteUser(new AuthorizationData("Tom", "qwerty"));
    }
    catch (Throwable e) {
      fail();
    }
    try {
      a.authorize(new AuthorizationData("Tom", "qwerty"));
    }
    catch (UserDoesNotExistException e) {
      return;
    }
    catch (Throwable e) {
      fail();
    }
    fail();
  }

  public void test113() {
    try {
      a.deleteUser(new AuthorizationData("Tom", "123456"));
    }
    catch (UserDoesNotExistException e) {
      return;
    }
    catch (Throwable e) {
      fail();
    }
    fail();
  }

  public void test114() {
    try {
      a.addUser(new AuthorizationData("Tom", "qwerty"));
    }
    catch (Throwable e) {
      fail();
    }
    try {
      a.deleteUser(new AuthorizationData("Tom", "123456"));
    }
    catch (WrongPasswordException e) {
      return;
    }
    catch (Throwable e) {
      fail();
    }
    fail();
  }

  public void test115() {
    try {
      a.changeUserName(new AuthorizationData("Tom", "qwerty"), "Tim");
    }
    catch (Throwable e) {
      fail();
    }
    try {
      a.authorize(new AuthorizationData("Tim", "qwerty"));
    }
    catch (Throwable e) {
      fail();
    }
  }

  public void test116() {
    try {
      a.changeUserName(new AuthorizationData("Tim", "123456"), "Tom");
    }
    catch (WrongPasswordException e) {
      return;
    }
    catch (Throwable e) {
      fail();
    }
    fail();
  }

  public void test117() {
    try {
      a.changeUserName(new AuthorizationData("Tom", "123456"), "Tim");
    }
    catch (UserDoesNotExistException e) {
      return;
    }
    catch (Throwable e) {
      fail();
    }
    fail();
  }

  public void test118() {
    try {
      a.changeUserPassword(new AuthorizationData("Tim", "qwerty"), "123456");
    }
    catch (Throwable e) {
      fail();
    }
    try {
      a.authorize(new AuthorizationData("Tim", "123456"));
    }
    catch (Throwable e) {
      fail();
    }
  }

  public void test119() {
    try {
      a.changeUserPassword(new AuthorizationData("Tim", "qwerty"), "123456");
    }
    catch (WrongPasswordException e) {
      return;
    }
    catch (Throwable e) {
      fail();
    }
    fail();
  }

  public void test120() {
    try {
      a.changeUserPassword(new AuthorizationData("Tom", "qwerty"), "123456");
    }
    catch (UserDoesNotExistException e) {
      return;
    }
    catch (Throwable e) {
      fail();
    }
    fail();
  }

  public void test121() {
    try {
      a.changeUserName(new AuthorizationData("Tim", "123456"), "Jack");
    }
    catch (UserAlreadyExistsException e) {
      return;
    }
    catch (Throwable e) {
      fail();
    }
    fail();
  }

  public void test122() {
    try {
      UserData user = a.authorize(new AuthorizationData("", "123456"));
      assertEquals(user.getId(), 0);
    }
    catch (Throwable e) {
      fail();
    }
  }

  public void test123() {
    try {
      UserData user = a.getUserData("Jack");
      assertEquals(user.getName().compareTo("Jack"), 0);
      assertEquals(user.getPassword().compareTo("123456"), 0);
    }
    catch (Throwable e) {
      fail();
    }
  }
}
